package com.Student;

import java.util.ArrayList;
import java.util.Scanner;

/*
学生功能类
*/
public class  StudentOperator {
    Student[] arr;

    public StudentOperator() {
        arr = new Student[]{
                new Student("中华小学1006", "小红", 10, "女", "13527779527", 80, 90, 100, "一班"),
                new Student("中华小学1007", "小蓝", 10, "男", "13527779528", 20, 50, 0, "一班"),
                new Student("中华小学1008", "小绿", 10, "男", "13527779529", 50, 60, 20, "二班"),
                new Student("中华小学1009", "小紫", 10, "女", "13527779517", 85, 90, 95, "一班"),
                new Student("中华小学1010", "小黑", 10, "女", "13527779537", 70, 90, 99, "二班")
        };
    }

    public void meanStart() {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("-----欢迎进入中华小学管理系统-------");
            System.out.println("请选择你的操作");
            System.out.println("1.展示所有学员");
            System.out.println("2.查询学生的平均分(只有数学,语文,英语学科)");
            System.out.println("3.根据班级查总分(只有数学,语文,英语学科)");
            System.out.println("4.根据班级查人数");
            System.out.println("5.根据班级查询平均分(只有数学,语文,英语学科)");
            System.out.println("6.查询总分最高的学生信息");
            System.out.println("7.退出系统");

            int choose = scanner.nextInt();
            switch (choose) {
                case 1 -> {
                    showAllStudentInfo();
                }
                case 2 -> {
                    System.out.println("请输入要查询的学科");
                    String subject = scanner.next();
                    double avg = getAllStudentAvg(subject);
                    System.out.println("你查询的" + subject + "学科,平均分是:" + avg);
                }
                case 3 -> {
                    System.out.println("请输入要查询的学科");
                    String subject = scanner.next();
                    System.out.println("请输入要查询的班级");
                    String clazz = scanner.next();
                    double sum = getSubjectTotalScoreByClass(subject, clazz);
                    System.out.println("你查询的" + clazz + "的" + subject + "学科的总分是:" + sum);
                }
                case 4 -> {
                    System.out.println("请输入要查询的班级");
                    String clazz = scanner.next();
                    int count = getTotalCountByClass(clazz);
                    System.out.println("你查询的" + clazz + "的人数是:" + count);

                }
                case 5 -> {
                    System.out.println("请输入要查询的学科");
                    String subject = scanner.next();
                    System.out.println("请输入要查询的班级");
                    String clazz = scanner.next();
                    double avg = getSubjectAvgByClass(subject, clazz);
                    System.out.println("你查询的" + clazz + "的" + subject + "学科的平均是:" + avg);
                }
                case 6 -> {
                    showTotalScoreMaxStudentInfo();
                }
                case 7 -> {
                    System.out.println("欢迎下次再来~~~");
                    return;
                }
            }
        }
    }

    public void showTotalScoreMaxStudentInfo() {
        //1.遍历数组, 查询每一个学生,  取出学生的三个分数, 加起来和临时变量比, 一旦比临时大就赋值给临时变量, 想办法将学生信息记录下来
        int max = 0;
        Student temp = null;
        ArrayList<Student> list=new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            int total = stu.getChinese() + stu.getMath() + stu.getEnglish();
            if (total >= max) {
                max = total;
                temp = stu;
            }
            if (total==max){
                list.add(stu);
            }
        }
        System.out.println("以下这些同学的总分是:" + max);
        for (int i = 0; i < list.size(); i++) {
            System.out.println("学生的编号:" + list.get(i).getId());
            System.out.println("学生的姓名:" + list.get(i).getName());
            System.out.println("学生的年龄:" + list.get(i).getAge());
            System.out.println("学生的性别:" + list.get(i).getGender());
            System.out.println("学生的电话:" + list.get(i).getPhone());
            System.out.println("学生的语文:" + list.get(i).getChinese());
            System.out.println("学生的数学:" + list.get(i).getMath());
            System.out.println("学生的英语:" + list.get(i).getEnglish());
            System.out.println("学生的班级:" + list.get(i).getClazz());
            System.out.println("=========================");
        }
    }

    public int getTotalCountByClass(String clazz) {
        //1.定义计数器
        int count = 0;
        //2.遍历数组
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (stu.getClazz().equals(clazz)) {
                count++;
            }
        }
        return count;

    }

    public double getSubjectAvgByClass(String subject, String clazz) {
        if (!subject.equals("语文") && !subject.equals("数学") && !subject.equals("英文")) {
            System.out.println("你二货吗???");
            return 0;
        }
        if (!clazz.equals("一班") && !clazz.equals("二班")) {
            System.out.println("你查询的班级不存在");
            return 0;
        }
        //1.定义求和变量
        double sum = getSubjectTotalScoreByClass(subject, clazz);
        int count = getTotalCountByClass(clazz);
        //3.算出总分
        return sum * 1.0 / count;

    }

    public double getSubjectTotalScoreByClass(String subject, String clazz) {
        if (!subject.equals("语文") && !subject.equals("数学") && !subject.equals("英文")) {
            System.out.println("你二货吗???");
            return 0;
        }
        if (!clazz.equals("一班") && !clazz.equals("二班")) {
            System.out.println("你查询的班级不存在");
            return 0;
        }
        //1.定义求和变量
        int sum = 0;
        //2.遍历所有的学生, 找到学生的subject学科的分数, 以及对应的班级, 累加到sum中
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (subject.equals("语文") && stu.getClazz().equals(clazz)) {
                sum += stu.getChinese();
            } else if (subject.equals("数学") && stu.getClazz().equals(clazz)) {
                sum += stu.getMath();
            } else if (subject.equals("英语") && stu.getClazz().equals(clazz)) {
                sum += stu.getEnglish();
            }
        }
        //3.算出总分
        return sum;

    }

    public double getAllStudentAvg(String subject) {
        if (!subject.equals("语文") && !subject.equals("数学") && !subject.equals("英文")) {
            System.out.println("你二货吗???");
            return 0;
        }
        //1.定义求和变量
        int sum = 0;
        //2.遍历所有的学生, 找到学生的subject学科的分数, 累加到sum中
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (subject.equals("语文")) {
                sum += stu.getChinese();
            } else if (subject.equals("数学")) {
                sum += stu.getMath();
            } else if (subject.equals("英语")) {
                sum += stu.getEnglish();
            }
        }
        //3.算出平均分
        return sum * 1.0 / arr.length;

    }

    public void showAllStudentInfo() {
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            System.out.println("学生的编号:" + stu.getId());
            System.out.println("学生的姓名:" + stu.getName());
            System.out.println("学生的年龄:" + stu.getAge());
            System.out.println("学生的性别:" + stu.getGender());
            System.out.println("学生的电话:" + stu.getPhone());
            System.out.println("学生的语文:" + stu.getChinese());
            System.out.println("学生的数学:" + stu.getMath());
            System.out.println("学生的英语:" + stu.getEnglish());
            System.out.println("学生的班级:" + stu.getClazz());
            System.out.println("=========================");
        }

    }
}
